<?php
require_once 'lib/libs.php';
session_start ();

// //importante. completar los campos que faltan!!!!!!
$smarty = getSmarty ();

if (isset ( $_REQUEST ['operacion'] )) {
	$operacion = $_REQUEST ['operacion'];
	
	if ($operacion == 'buscarxfiltro') {
		
		$criterio = $_REQUEST ['criterioSearch'];
		
		$usuarios = DAOUsuario::getByFilter($criterio);		
		echo json_encode ($usuarios);
		return;
		
	}
	if ($operacion == 'quickSearch') {
		// Define Output HTML Formating
		
		$filter = $_REQUEST ['searchFilter'];
		
		$html = '';
		$html .= '<li class="result">';
		
		$html .= '<a href="javascript:setUsr(usrId);">';
		$html .= '<h5>apellidoString, nombreString - DNI: dniString</h5>';
		$html .= '</a>';
		$html .= '</li>';

		if (strlen ( $filter ) >= 1 && $filter !== ' ') {
			$usuarios = DAOUsuario::getByFilter ( $filter );		

			// Check If We Have Results
			if (isset($usuarios)) {
				foreach ($usuarios as $result) {
			
					// Format Output Strings And Hightlight Matches
					$apellido = preg_replace("/".$filter."/i", "<b class='highlight'>".$filter."</b>", $result->getApellido ());
					$nombre = preg_replace("/".$filter."/i", "<b class='highlight'>".$filter."</b>", $result->getNombre ());
					$dni = preg_replace("/".$filter."/i", "<b class='highlight'>".$filter."</b>", $result->getDni ());
// 					$dni = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';
					$usrId =  $result->getId() ;
			
					$output = str_replace('nombreString', $nombre, $html);
					$output = str_replace('apellidoString', $apellido, $output);
					$output = str_replace('dniString', $dni, $output);
					$output = str_replace('usrId', $usrId , $output);
					
					// Output
					echo($output);
				}
			}else{
			
				// Format No Results Output
				$output = str_replace('urlString', 'javascript:void(0);', $html);
				$output = str_replace('nombreString', '<b>Sin coincidencias</b>', $output);
				$output = str_replace('apellidoString', ' :(', $output);
				$output = str_replace('dniString', ' ', $output);
			
				// Output
				echo($output);
			}
			
// 			echo json_encode ( $usuarios );
		}
		return;
	}
	if ($operacion == 'getById') {
		$id = $_REQUEST ['idUsr'];
		$usr = DAOUsuario::getById ( $id );
		echo json_encode ( $usr );
		return;
	}if ($operacion == 'cargaManual') {
		$id = $_REQUEST ['idUsr'];
		$fecha = $_REQUEST ['fecha'];
		$cantidad = $_REQUEST ['cantidad'];
		$usr = DAOUsuario::getById ( $id );
		$usr->setVencimientoCreditos ( DateUtils::getMysqlDate ( DateTime::createFromFormat("d/m/Y H:i:s", $fecha." 23:59:59")->getTimestamp() ));
		$usr->setCreditos ( $cantidad );
		if($_REQUEST ['libre'] == 'true'){
			$usr->setLibre ( 'S' );
		}else{
			$usr->setLibre ( 'N' );
		}
		DAOUsuario::update ( $usr );	
		$usr = DAOUsuario::getById ( $id );
		echo json_encode ( $usr );
		return;
	}if ($operacion == 'cargaCredito') {
		$id = $_REQUEST ['idUsrCarga'];
		$usr = DAOUsuario::getById ( $id );
		$antiguos = $usr->getCreditos ();
		$promo = DAOPromocion::getById ( $_REQUEST ['promoId'] );
		
		$aCargar = $promo->creditos;
		$precio = $promo->precio;
		if($promo->libre=='S'){
			$usr->setCreditos (-1);
		}else{
			$usr->setCreditos ( $aCargar );
		}
		$usr->setLibre ( $promo->libre );
		$dt = new DateTime;
		$dt->setTime(23,59,59);
		$dt->add(new DateInterval('P'.$promo->meses.'M'));
		
		
		$usr->setVencimientoCreditos ( DateUtils::getMysqlDate ( $dt->getTimestamp() ));
		
		DAOUsuario::update ( $usr );
		
		$historicoCarga = new HistoricoCarga ();
		$historicoCarga->setUsuario ( $id );
		$historicoCarga->setAntiguos ( $antiguos );
		$historicoCarga->setCreditos ( $aCargar );
		$historicoCarga->setPrecio ( $precio );
		$historicoCarga->setLibre ( $promo->libre );
		$historicoCarga->setVencimientoCreditos ( DateUtils::getMysqlDate ( $dt->getTimestamp()) );
		$historicoCarga->setFechaCarga ( DateUtils::getNowMysqlDate () );
		$hca = DAOHistoricoCarga::add ( $historicoCarga );
		$usr->setVencimientoCreditosStr ( date ( "d/m/Y", strtotime ( $usr->vencimientoCreditos ) ) );
		echo json_encode ( $usr );
		return;
	}
	if ($operacion == 'getHistory') {
		$idUsr = $_REQUEST ['idUsr'];
		$historicos = DAOHistoricoCarga::getAllByUsuario ( $idUsr );
		echo json_encode ( $historicos );
		return;
	}
}


$username = $_SESSION ['ADM_USR']->getNombre ();
$smarty->assign ( 'username', $username );
$smarty->display ( 'admin.html' );

?>